home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / X-Ray / samples / multi win test / SillyBalls.c next >
Encoding:
C/C++ Source or Header  |  1999-06-11  |  5.1 KB  |  210 lines  |  [TEXT/CWIE]

  1. // Copyright (C) 1999 Eric Roccasecca.  All rights reserved.
  2. // Based on Sillyballs, Copyright (C) 1988 Apple Computer, Inc.
  3.  
  4. #include "X_Ray.h"
  5.  
  6. #define BallWidth        20
  7. #define BallHeight        20
  8. #define BobSize            8
  9.  
  10. Rect        windRect;
  11. WindowPtr    main1Ptr;
  12. WindowPtr    main2Ptr;
  13. WindowPtr    main3Ptr;
  14. Boolean        gDone = false;
  15. Boolean        isBig = false;
  16.  
  17. void Initialize(void);
  18. void MakeWindows (void);
  19. void NewBall(void);
  20.  
  21. int main(void)
  22. {
  23.     OSErr            err;
  24.     EventRecord        theEvent;
  25.     GrafPtr            curPort;
  26.     
  27.     Initialize();
  28.     err = InitTransparentWindows();
  29.     MakeWindows();
  30.     
  31.     while (!gDone)
  32.     {
  33.         WaitNextEvent (everyEvent, &theEvent, 10, nil);
  34.         if (theEvent.what == keyDown)
  35.         {
  36.             if ((theEvent.message & charCodeMask) == ' ')
  37.             {
  38.                 GetPort (&curPort);
  39.                 
  40.                 SetPort (main2Ptr);
  41.                 EraseRect (&main2Ptr->portRect);
  42.                 SetPort (main1Ptr);
  43.                 EraseRect (&main1Ptr->portRect);
  44.                 SetPort (main3Ptr);
  45.                 EraseRect (&main3Ptr->portRect);
  46.                 
  47.                 SetPort (curPort);
  48.             }
  49.             else if ((theEvent.message & charCodeMask) == '1')    // erase window one
  50.             {
  51.                 GetPort (&curPort);
  52.                 SetPort (main1Ptr);
  53.                 EraseRect (&main1Ptr->portRect);
  54.                 SetPort (curPort);
  55.             }
  56.             else if ((theEvent.message & charCodeMask) == '2')    // erase window two
  57.             {
  58.                 GetPort (&curPort);
  59.                 SetPort (main2Ptr);
  60.                 EraseRect (&main2Ptr->portRect);
  61.                 SetPort (curPort);
  62.             }
  63.             else if ((theEvent.message & charCodeMask) == '3')    // erase window three
  64.             {
  65.                 GetPort (&curPort);
  66.                 SetPort (main3Ptr);
  67.                 EraseRect (&main3Ptr->portRect);
  68.                 SetPort (curPort);
  69.             }
  70.             else if ((theEvent.message & charCodeMask) == 'a')    // bring window 1 to front
  71.                 SelectWindow (main1Ptr);
  72.             else if ((theEvent.message & charCodeMask) == 's')    // bring window 2 to front
  73.                 SelectWindow (main2Ptr);
  74.             else if ((theEvent.message & charCodeMask) == 'd')    // bring window 3 to front
  75.                 SelectWindow (main3Ptr);
  76.             else if ((theEvent.message & charCodeMask) == 'b')    // toggle window 3 size
  77.             {
  78.                 if (isBig)
  79.                 {
  80.                     SizeWindow (main3Ptr, (main3Ptr->portRect.right - main3Ptr->portRect.left) - 50, (main3Ptr->portRect.bottom - main3Ptr->portRect.top) - 50, false);
  81.                     isBig = false;
  82.                 }
  83.                 else
  84.                 {
  85.                     SizeWindow (main3Ptr, (main3Ptr->portRect.right - main3Ptr->portRect.left) + 50, (main3Ptr->portRect.bottom - main3Ptr->portRect.top) + 50, false);
  86.                     isBig = true;
  87.                 }
  88.             }
  89.             else if ((theEvent.message & charCodeMask) == 'q')    // quit
  90.                 gDone = true;
  91.         }
  92.         else if (theEvent.what == mouseDown)
  93.         {
  94.             short        foundWindPart;
  95.             WindowPtr    foundWind;
  96.             
  97.             foundWindPart = FindWindow (theEvent.where, &foundWind);
  98.             if (foundWind)
  99.             {
  100.                 if (foundWindPart == inDrag)
  101.                     DragWindow (foundWind, theEvent.where, &(*LMGetGrayRgn())->rgnBBox);
  102.                 else if (foundWindPart == inContent)
  103.                     SelectWindow (foundWind);
  104.             }
  105.         }
  106.         else if (theEvent.what == updateEvt)
  107.         {
  108.             GetPort (&curPort);
  109.             SetPort ((WindowPtr)theEvent.message);
  110.             
  111.             BeginUpdate ((WindowPtr)theEvent.message);
  112.             EraseRect (&((WindowPtr)theEvent.message)->portRect);
  113.             EndUpdate ((WindowPtr)theEvent.message);
  114.             
  115.             SetPort (curPort);
  116.         }
  117.         else if (theEvent.what == nullEvent)
  118.         {
  119.             GetPort (&curPort);
  120.             
  121.             if (curPort == main1Ptr)
  122.                 SetPort (main2Ptr);
  123.             else if (curPort == main2Ptr)
  124.                 SetPort (main3Ptr);
  125.             else if (curPort == main3Ptr)
  126.                 SetPort (main1Ptr);
  127.             
  128.             NewBall();
  129.         }
  130.     }
  131.     
  132.     DisposeTransparentWindow (main1Ptr);
  133.     DisposeTransparentWindow (main2Ptr);
  134.     DisposeTransparentWindow (main3Ptr);
  135.     
  136.     return 0;    
  137. }
  138.  
  139. void MakeWindows (void)
  140. {
  141.     OSErr        error;
  142.     
  143.     SetRect (&windRect, 50, 50, 250, 250);
  144.     main1Ptr = NewTransparentWindow (nil, &windRect, "\pTransparent 1", true, documentProc, (WindowPtr) -1, false, 0, nil, nil, &error);
  145.     OffsetRect (&windRect, 50, 50);
  146.     main2Ptr = NewCWindow (nil, &windRect, "\pNormal 2", true, documentProc, (WindowPtr) -1, false, 0);
  147.     //main2Ptr = NewTransparentWindow (nil, &windRect, "\pTransparent 2", true, documentProc, (WindowPtr) -1, false, 0, nil, nil, &error);
  148.     OffsetRect (&windRect, 50, 50);
  149.     main3Ptr = NewTransparentWindow (nil, &windRect, "\pTransparent 3", true, documentProc, (WindowPtr) -1, false, 0, nil, nil, &error);
  150.         
  151.     SetPort (main1Ptr);
  152.     TextSize (BobSize);
  153. }
  154.  
  155.  
  156. void Initialize (void)
  157. {    
  158.     InitGraf (&qd.thePort);
  159.     InitFonts();
  160.     InitWindows();
  161.     InitMenus();
  162.     TEInit();
  163.     InitDialogs (nil);
  164.     InitCursor();
  165.  
  166.     GetDateTime((unsigned long*) &qd.randSeed);
  167. }
  168.  
  169.  
  170. void NewBall(void)
  171. {
  172.     RGBColor    ballColor;
  173.     Rect        ballRect;
  174.     long int    newLeft,
  175.                 newTop;
  176.     GrafPtr        curPort;
  177.     
  178.     GetPort (&curPort);
  179.     
  180.     if (curPort == main1Ptr)
  181.     {
  182.         ballColor.red   = 0xFFFF;
  183.         ballColor.green = 0;
  184.         ballColor.blue  = 0;
  185.     }
  186.     else if (curPort == main2Ptr)
  187.     {
  188.         ballColor.red   = 0;
  189.         ballColor.green = 0;
  190.         ballColor.blue  = 0xFFFF;
  191.     }
  192.     else if (curPort == main3Ptr)
  193.     {
  194.         ballColor.red   = 0;
  195.         ballColor.green = 0xFFFF;
  196.         ballColor.blue  = 0;
  197.     }
  198.     
  199.     RGBForeColor (&ballColor);
  200.  
  201.     newTop = Random();
  202.     newLeft = Random();
  203.     newTop = ((newTop+32767) * windRect.bottom)/65536;
  204.     newLeft = ((newLeft+32767) * windRect.right)/65536;
  205.     SetRect (&ballRect, (short)newLeft, (short)newTop, (short)(newLeft+BallWidth), (short)(newTop+BallHeight));
  206.     
  207.     MoveTo ((short)newLeft, (short)newTop);
  208.     PaintOval (&ballRect);
  209. }
  210.